package com.magenta.maxoptra.gis;

import org.jetbrains.annotations.Nullable;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Sergey Grachev
 */
public final class PostalCodeDistanceMatrix implements Serializable {

    private static final long serialVersionUID = -4009134395231011589L;

    private final Map<Object, Map<Object, GeoRoute>> matrix = new HashMap<Object, Map<Object, GeoRoute>>();

    @Nullable
    public GeoRoute get(final Object from, final Object to) {
        final Map<Object, GeoRoute> routes = matrix.get(from);
        return routes != null ? routes.get(to) : null;
    }

    public void add(final Object from, final Map<Object, GeoRoute> routes) {
        Map<Object, GeoRoute> existsRoutes = matrix.get(from);
        if (existsRoutes == null) {
            existsRoutes = new HashMap<Object, GeoRoute>(routes);
            matrix.put(from, existsRoutes);
        } else {
            existsRoutes.putAll(routes);
        }
    }
}
